package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.House;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.VO.OfficeBHVO;
import com.zhongxiangspace.entity.VO.QueryAnalyzerOffice;
import com.zhongxiangspace.entity.VO.TradeAreaVO;
import com.zhongxiangspace.enums.*;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.*;
import com.zhongxiangspace.tools.AnalyzerConditionTools;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OfficeListController {
    @Autowired
    private ReadProperties readProperties;

    @Autowired
    private BannerService bannerService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private AdvertService advertService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private ArticleService articleService;

    @GetMapping("lists/h1{value}")
    public String offficeList(@PathVariable("value") String value, Model model) {

     //   return "redirect:/lists/h1" + value + "/pv1";
        return  officeList(value,"pv1","",model);
    }
    @GetMapping("lists/h1{value}/{page}")
    public String offficeList(@PathVariable("value") String value, @PathVariable(value = "page") String page, Model model) {
        return  officeList(value,page,"",model);
    }
    @GetMapping("lists/h1{value}/{page}/{keyword}")
    public String officeList(@PathVariable("value") String value, @PathVariable(value = "page") String page,  @PathVariable(value = "keyword") String keyword,Model model) {
        Map<String, Object> map = new HashMap<>();
        if (page == "" || page.length() == 0) {
            page = "pv1";
        }
        model.addAttribute("keyword",keyword);
        Integer pageSize = PagesEnum.getEnumObject(page).getIndex();
        map.put("url", value);
        String seoAttributeJoint="";
        String regions = AnalyzerConditionTools.getCondition(value, "r");//缓存区域id
        map.put("regions", regions);
        if(!regions.isEmpty()){
           County county = this.countyService.queryOne(Integer.valueOf(regions.substring(1, regions.length())));
            seoAttributeJoint=seoAttributeJoint+""+county.getName();
        }
        String area = AnalyzerConditionTools.getCondition(value, "a");//缓存面积id
        String employees = AnalyzerConditionTools.getCondition(value, "e");//缓存人数
        if (!area.isEmpty()) {
            map.put("area", area);
        } else {
            map.put("area", employees);
        }
        String price = AnalyzerConditionTools.getCondition(value, "p");//缓存金额
        map.put("price", price);
        String decorate = AnalyzerConditionTools.getCondition(value, "d");//缓存装修
        map.put("decorate", decorate);
        QueryAnalyzer tradeAreaQueryAnalyzer = new QueryAnalyzer();
        tradeAreaQueryAnalyzer.setShowCode(1);
        tradeAreaQueryAnalyzer.setHotCode(1);
        if (!regions.isEmpty()) {
            tradeAreaQueryAnalyzer.setType(regions.substring(1, regions.length()));
            List<Map<String, Object>> tradeAreaVOS = this.countyService.queryTradeAreaByKeyword(tradeAreaQueryAnalyzer);
            map.put("tradeArea", tradeAreaVOS);
        }
        String tradeArea_id = AnalyzerConditionTools.getCondition(value, "t");//缓存商圈
        if(!tradeArea_id.isEmpty()){
            System.out.println(tradeArea_id.substring(1, tradeArea_id.length()));
            County county = this.countyService.queryOne(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
            seoAttributeJoint=seoAttributeJoint+""+county.getName();
        }
        map.put("tradeArea_id", tradeArea_id);
        //区域
        QueryAnalyzer countQueryAnalyzer = new QueryAnalyzer();
        countQueryAnalyzer.setShowCode(1);
        countQueryAnalyzer.setType("0");
        List<County> countyList = this.countyService.queryByKeyword(countQueryAnalyzer);
        map.put("county", countyList);
        /*******************查询条件**************************************************/
        QueryAnalyzerOffice queryAnalyzerOffice = new QueryAnalyzerOffice();
        queryAnalyzerOffice.setPage(pageSize);
        //区域
        if (!regions.isEmpty()) {
            queryAnalyzerOffice.setCounty_id(Integer.valueOf(regions.substring(1, regions.length())));
        }
        //商圈
        if (!tradeArea_id.isEmpty()) {
            queryAnalyzerOffice.setTradeArea_id(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
        }
        //装修
        if (!decorate.isEmpty()) {
            queryAnalyzerOffice.setDecorate(String.valueOf(DecorationEnum.getEnumObject(decorate).getValue()));
            seoAttributeJoint=seoAttributeJoint+DecorationEnum.getEnumObject(decorate).getValue();
        }
        String areaStr="";
        //面积
        if (!area.isEmpty()) {
            queryAnalyzerOffice.setMinArea(AreaExtentEnum.getEnumObject(area).getMin());
            queryAnalyzerOffice.setMaxArea(AreaExtentEnum.getEnumObject(area).getMax());
            areaStr=AreaExtentEnum.getEnumObject(area).getMin()+"-"+AreaExtentEnum.getEnumObject(area).getMax()+"㎡";
            if(AreaExtentEnum.getEnumObject(area).getMax()>=1001){
                areaStr=AreaExtentEnum.getEnumObject(area).getMin()+"㎡以上";
            }
        } else if (!employees.isEmpty()) {
            queryAnalyzerOffice.setMinArea(EmployeesEnum.getEnumObject(employees).getMin() * 8);
            queryAnalyzerOffice.setMaxArea(EmployeesEnum.getEnumObject(employees).getMax() * 15);
            areaStr=EmployeesEnum.getEnumObject(employees).getMin()+"-"+EmployeesEnum.getEnumObject(employees).getMax()+"人";
            if(EmployeesEnum.getEnumObject(employees).getMax()>=51){
                areaStr=EmployeesEnum.getEnumObject(employees).getMin()+"人以上";
            }
        }
        seoAttributeJoint=seoAttributeJoint+areaStr;
        //单价
        if (!price.isEmpty()) {
            queryAnalyzerOffice.setMinPrice(PriceExtenEnum.getEnumObject(price).getMin());
            queryAnalyzerOffice.setMaxPrice(PriceExtenEnum.getEnumObject(price).getMax());
            String    priceStr=PriceExtenEnum.getEnumObject(price).getMin()+"-"+PriceExtenEnum.getEnumObject(price).getMax()+"元";
            if(PriceExtenEnum.getEnumObject(price).getMax()>=201){
                priceStr=PriceExtenEnum.getEnumObject(price).getMin()+"元以上";
            }
            seoAttributeJoint=seoAttributeJoint+priceStr;
        }

        /*****************************主楼盘数据**********************************/
        if(keyword!=null&& !keyword.isEmpty()){
            queryAnalyzerOffice.setKeyword(keyword);
        }
        PageHelper.startPage(queryAnalyzerOffice.getPage(),16 ,"office_buildings.hotCode DESC");
        Page<OfficeBHVO> officeBHVOPage = (Page<OfficeBHVO>) this.officeBuildingService.queryOfficeBH(queryAnalyzerOffice);
        List<OfficeBHVO> officeBHVOList = officeBHVOPage.getResult();
        for (OfficeBHVO office : officeBHVOList) {
            queryAnalyzerOffice.setBuilding_id(office.getBuilding_id());
            queryAnalyzerOffice.setDistinct("coveredArea");
            List<House> houseList = this.houseService.queryBHList(queryAnalyzerOffice);
            office.setHouses(houseList);
        }
        PageResult pageResult = new PageResult(officeBHVOPage.getPageNum(), officeBHVOPage.getPageSize(), officeBHVOPage.getTotal(), officeBHVOPage.getPages(), officeBHVOPage.getResult(), true);
        map.put("officeData", pageResult);
        long total=pageResult.getTotal();
        model.addAttribute("pageNum",pageResult.getPageNum());
        model.addAttribute("totall",total);
        /*******seo属性**********/
        model.addAttribute("seoAttributeJoint",seoAttributeJoint);
        /************************广告位*********************************************/
        QueryAnalyzer advertQueryAnalyzer = new QueryAnalyzer();
        advertQueryAnalyzer.setShowCode(1);
        List<Map<String, Object>> advertList = this.advertService.queryAdvert(advertQueryAnalyzer);
        map.put("advertList", advertList);
        /************************热门楼盘*********************************************/
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        /*******************记录结果******************************************/
        model.addAttribute("data", map);
        return "list/office_list";
    }





}
